package org.example.myframework.frame.common.exception;

import org.example.myframework.frame.common.entity.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理类
 */


@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

/**
     * 处理系统层异常信息
     */

    @ExceptionHandler(BaseException.class)
    @ResponseBody
    public R baseExceptionHandler(BaseException e) {
        return R.error(e.getCode(), e.getMsg(), null);
    }

/**
     * 处理业务层异常信息
     */

    @ExceptionHandler(SystemException.class)
    @ResponseBody
    public R systemExceptionHandler(SystemException e) {
        logger.error("");
        logger.error("系统请求异常，报错为：{}", e.getMsg());
        return R.error(e.getCode(), e.getMsg(), null);
    }


    /**
     * 全局异常处理，自定义RuntimeException捕获
     * @param e
     * @return
     */

        @ExceptionHandler({Exception.class,RuntimeException.class})
        public R RuntimeException(RuntimeException e) {
            return R.fail(e.getMessage());
        }


}
